import 'package:app/common/colors.dart';
import 'package:app/components/countdown_buttother_page.dart';
import 'package:app/services/address.dart';
import 'package:app/services/dio_manager.dart';
import 'package:app/utils/hexcolor.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

class MineSecurityPage extends StatefulWidget {
  @override
  _MineSecurityPageState createState() => _MineSecurityPageState();
}

final TextEditingController textEditingContr0 = TextEditingController();
final TextEditingController textEditingContr1 = TextEditingController();
final TextEditingController textEditingContr2 = TextEditingController();
final TextEditingController textEditingContr3 = TextEditingController();
final TextEditingController textEditingContr5 = TextEditingController();

/// 忘记密码请求
requestDataWithChangePassword(var mobile, var password, var code) async {
  var params = {
    'mobile': mobile.replaceAll(new RegExp(r"\s+\b|\b\s"), ""),
    'password': password,
    'code': code,
  };

  var json =
      await DioManager().kkRequest(Address.forgetPassword, bodyParams: params,isShowLoad: true);
  if (json['errmsg'] == 'ok') {
    BotToast.showText(text: '${json['message']}');
    Get.back();
  }
  BotToast.showText(text: '${json['message']}');
}

class _MineSecurityPageState extends State<MineSecurityPage>
    with TickerProviderStateMixin {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 2,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('账号安全', style: TextStyle(color: Colors.black)),
          backgroundColor: Color(0xFFfafcff),
          bottom: const TabBar(
              labelColor: Color(0xff030303),
              indicatorColor: Colors.grey,
              indicatorSize: TabBarIndicatorSize.label,
              tabs: [
                Tab(
                  text: "手机号验证",
                ),
                Tab(
                  text: "密码验证",
                )
              ]),
        ),
        body: TabBarView(children: [
          Container(
              decoration: BoxDecoration(
                color: Color(0xffffffff),
                borderRadius: BorderRadius.circular(20),
              ),
              child: ShowEffect()),
          Container(
              decoration: BoxDecoration(
                color: Color(0xffffffff),
                borderRadius: BorderRadius.circular(20),
              ),
              child: ShowAttribute()),
        ]),
      ),
    );
  }
}

// 效果
class ShowEffect extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Align(
          alignment: Alignment.center,
          child: Center(
            child: ListView(
              children: [
                Container(
                    padding: const EdgeInsets.all(12),
                    margin: const EdgeInsets.only(top: 24, right: 12, left: 12),
                    child: Row(
                      children: [
                        IconButton(
                            onPressed: () {},
                            icon: Image.asset(
                              'images/mobile_icon.png',
                              width: 18,
                              height: 18,
                            )),
                        SizedBox(
                          width: Get.width - 240,
                          child: TextField(
                            keyboardType: TextInputType.phone,
                            obscureText: false,
                            controller: textEditingContr0,
                            style: const TextStyle(
                                fontSize: 16, color: Colors.black),
                            inputFormatters: <TextInputFormatter>[
                              FilteringTextInputFormatter.digitsOnly, //只输入数字
                              LengthLimitingTextInputFormatter(12)
                              //限制长度
                            ],
                            decoration: InputDecoration(
                              // contentPadding: EdgeInsets.only(top: 15),
                              hintText: '请输入手机号'.tr,
                              hintStyle: const TextStyle(
                                  fontSize: 16, color: Colors.grey),
                              border: InputBorder.none,
                            ),
                          ),
                        ),
                        CountOtherButton(textEditingContr0)
                      ],
                    )),
                Container(
                  color: HexColor("#EAEAEA"),
                  child: SizedBox(
                    height: 1,
                    width: Get.width - 32,
                  ),
                ),
                Container(
                    padding: const EdgeInsets.all(12),
                    margin: const EdgeInsets.only(top: 24, right: 12, left: 12),
                    child: Row(
                      children: [
                        IconButton(
                            onPressed: () {},
                            icon: Image.asset(
                              'images/yzm_icon.png',
                              width: 18,
                              height: 18,
                              // package: 'flutter_public_widget',
                            )),
                        SizedBox(
                          width: Get.width - 100,
                          child: TextField(
                            keyboardType: TextInputType.number,
                            obscureText: false,
                            controller: textEditingContr1,
                            style: const TextStyle(
                                fontSize: 16, color: Colors.black),
                            inputFormatters: <TextInputFormatter>[
                              FilteringTextInputFormatter.digitsOnly, //只输入数字
                              LengthLimitingTextInputFormatter(8)
                              //限制长度
                            ],
                            decoration: InputDecoration(
                              // contentPadding: EdgeInsets.only(top: 15),
                              hintText: '请输入验证码'.tr,
                              hintStyle: const TextStyle(
                                  fontSize: 16, color: Colors.grey),
                              border: InputBorder.none,
                              // prefixIcon: Image.asset(A.assets_pwd),
                            ),
                          ),
                        )
                      ],
                    )),
                Container(
                  color: HexColor("#EAEAEA"),
                  child: SizedBox(
                    height: 1,
                    width: Get.width - 32,
                  ),
                ),
                Container(
                    padding: const EdgeInsets.all(12),
                    margin: const EdgeInsets.only(top: 24, right: 12, left: 12),
                    child: Row(
                      children: [
                        IconButton(
                            onPressed: () {},
                            icon: Image.asset(
                              'images/pass_icon.png',
                              width: 18,
                              height: 18,
                              // package: 'flutter_public_widget',
                            )),
                        SizedBox(
                          width: Get.width - 100,
                          child: TextField(
                            keyboardType: TextInputType.text,
                            obscureText: true,
                            controller: textEditingContr2,
                            style: const TextStyle(
                                fontSize: 16, color: Colors.black),
                            inputFormatters: <TextInputFormatter>[
                              LengthLimitingTextInputFormatter(16)
                              //限制长度
                            ],
                            decoration: InputDecoration(
                              // contentPadding: EdgeInsets.only(top: 15),
                              hintText: '请输入新密码'.tr,
                              hintStyle: const TextStyle(
                                  fontSize: 16, color: Colors.grey),
                              border: InputBorder.none,
                              // prefixIcon: Image.asset(A.assets_pwd),
                            ),
                          ),
                        )
                      ],
                    )),
                Container(
                  color: HexColor("#EAEAEA"),
                  child: SizedBox(
                    height: 1,
                    width: Get.width - 32,
                  ),
                ),
                Container(
                  height: 44,
                  margin: const EdgeInsets.only(top: 12, right: 12, left: 12),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Container(
                          padding:
                              const EdgeInsets.only(left: 4, top: 6, right: 4),
                          child: const Text(
                            "*",
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                color: Colors.blueAccent, fontSize: 16),
                          )),
                      const Text(
                        "8-16位密码，须包含数字、字母、符号中至少2位元素",
                        style: TextStyle(color: Colors.grey, fontSize: 12),
                      )
                    ],
                  ),
                ),
                Container(
                  margin: const EdgeInsets.only(top: 48, right: 21, left: 21),
                  width: Get.width,
                  height: 48,
                  child: MaterialButton(
                    elevation: 0,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(24),
                    ),
                    color: AppColor.themeColor,
                    onPressed: () async {
                      if (textEditingContr0.text.isEmpty) {
                        BotToast.showText(text: '请输入手机号码');
                        return;
                      }
                      if (!GetUtils.isPhoneNumber(textEditingContr0.text)) {
                        BotToast.showText(text: '请输入正确的手机号码');
                        return;
                      }
                      if (textEditingContr1.text.isEmpty) {
                        BotToast.showText(text: '请输入验证码');
                        return;
                      }
                      if (textEditingContr2.text.isEmpty) {
                        BotToast.showText(text: '请输入新密码');
                        return;
                      }
                      requestDataWithChangePassword(textEditingContr0.text,
                          textEditingContr2.text, textEditingContr1.text);
                    },
                    child: Text(
                      '确认'.tr,
                      style: const TextStyle(color: Colors.white),
                    ),
                  ),
                ),
              ],
            ),
          )),
    );
  }
}

// 属性
class ShowAttribute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Align(
          alignment: Alignment.center,
          child: Center(
            child: ListView(
              children: [
                Container(
                    padding: const EdgeInsets.all(12),
                    margin: const EdgeInsets.only(top: 24, right: 12, left: 12),
                    child: Row(
                      children: [
                        IconButton(
                            onPressed: () {},
                            icon: Image.asset(
                              'images/pass_icon.png',
                              width: 18,
                              height: 18,
                              // package: 'flutter_public_widget',
                            )),
                        SizedBox(
                          width: Get.width - 100,
                          child: TextField(
                            keyboardType: TextInputType.text,
                            obscureText: false,
                            controller: textEditingContr3,
                            style: const TextStyle(
                                fontSize: 16, color: Colors.black),
                            inputFormatters: <TextInputFormatter>[
                              LengthLimitingTextInputFormatter(16)
                              //限制长度
                            ],
                            decoration: InputDecoration(
                              // contentPadding: EdgeInsets.only(top: 15),
                              hintText: '请输入旧密码'.tr,
                              hintStyle: const TextStyle(
                                  fontSize: 16, color: Colors.grey),
                              border: InputBorder.none,
                              // prefixIcon: Image.asset(A.assets_pwd),
                            ),
                          ),
                        )
                      ],
                    )),
                Container(
                  color: HexColor("#EAEAEA"),
                  child: SizedBox(
                    height: 1,
                    width: Get.width - 32,
                  ),
                ),
                Container(
                    padding: const EdgeInsets.all(12),
                    margin: const EdgeInsets.only(top: 24, right: 12, left: 12),
                    child: Row(
                      children: [
                        IconButton(
                            onPressed: () {},
                            icon: Image.asset(
                              'images/pass_icon.png',
                              width: 18,
                              height: 18,
                              // package: 'flutter_public_widget',
                            )),
                        SizedBox(
                          width: Get.width - 100,
                          child: TextField(
                            keyboardType: TextInputType.text,
                            obscureText: false,
                            controller: textEditingContr5,
                            style: const TextStyle(
                                fontSize: 16, color: Colors.black),
                            inputFormatters: <TextInputFormatter>[
                              LengthLimitingTextInputFormatter(16)
                              //限制长度
                            ],
                            decoration: InputDecoration(
                              // contentPadding: EdgeInsets.only(top: 15),
                              hintText: '请输入新密码'.tr,
                              hintStyle: const TextStyle(
                                  fontSize: 16, color: Colors.grey),
                              border: InputBorder.none,
                              // prefixIcon: Image.asset(A.assets_pwd),
                            ),
                          ),
                        )
                      ],
                    )),
                Container(
                  color: HexColor("#EAEAEA"),
                  child: SizedBox(
                    height: 1,
                    width: Get.width - 32,
                  ),
                ),
                Container(
                  height: 44,
                  margin: const EdgeInsets.only(top: 12, right: 12, left: 12),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Container(
                          padding:
                              const EdgeInsets.only(left: 4, top: 6, right: 4),
                          child: const Text(
                            "*",
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                color: Colors.blueAccent, fontSize: 16),
                          )),
                      const Text(
                        "8-16位密码，须包含数字、字母、符号中至少2位元素",
                        style: TextStyle(color: Colors.grey, fontSize: 12),
                      )
                    ],
                  ),
                ),
                Container(
                  margin: const EdgeInsets.only(top: 48, right: 21, left: 21),
                  width: Get.width,
                  height: 48,
                  child: MaterialButton(
                    elevation: 0,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(24),
                    ),
                    color: AppColor.themeColor,
                    onPressed: () => {},
                    child: Text(
                      '确认'.tr,
                      style: const TextStyle(color: Colors.white),
                    ),
                  ),
                ),
              ],
            ),
          )),
    );
  }
}
